package khleuven.mondzorg.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

// TODO Refactor naar MondApp Singleton: alle unieke illustraties
// TODO Als illustratie met ID niet gekend -> vraag op aan WWW
// TODO Als illustratie met ID niet gekend op WWW -> lege byte[]
// TODO GUI Als lege byte[] toon enkel de tekst (explanation)
public class Illustrations {

	// Refactoring done from ArrayList to HashMap to make easy lookup on illustration id possible
	private LinkedHashMap<String, Illustration> illustrations;
	
	// TODO als een illustratie key nog niet bestaat, een "niet gekend prentje" laten zien.
	// private Illustration dummyIllustration; 
	
	public Illustrations(){
		setIllustrations(new LinkedHashMap<String, Illustration>());
	}

	public Illustrations(JSONArray jsonArray) throws DomainException {
		this();
		try {
			for(int i=0;i<jsonArray.length();i++){
				JSONObject json_data;
				json_data = jsonArray.getJSONObject(i);
				add(new Illustration(json_data));
			}
		} catch (JSONException e) {
			throw new DomainException("Something went wrong while translating from json",e);
		}
	}
	
	public Illustrations(String illustrationids) throws DomainException {
		this();
		if(illustrationids.length() > 0) {
			String[] illids = illustrationids.split(";");
			for(int i = 0; i < illids.length; i++){
				Illustration ill = DentalCareApp.getInstance().getIllustration(illids[i]);
				add(ill);
			}	
		}
	}

	public ArrayList<Illustration> getIllustrations() {
		ArrayList<Illustration> returns = new ArrayList<Illustration>();
		returns.addAll(illustrations.values()); // encapsulation
		return returns;
	}

	private void setIllustrations(LinkedHashMap<String, Illustration> illustrations) {
		this.illustrations = illustrations;
	}

	public void add(Illustration illustration){
		// getIllustrations is encapsulated - working with the instance variable directly
		this.illustrations.put(illustration.getFileName(), illustration);
	}
	
	@Override
	public boolean equals(Object o){
		boolean gelijk = false;
		if(o instanceof Illustrations){			
			if(((Illustrations)(o)).getIllustrations().containsAll(getIllustrations())){
				gelijk = true;
			}
		}
		return gelijk;
	}
	
	public JSONArray toJSonArray() throws DomainException{
		JSONArray output = new JSONArray();
		for(Illustration ill: illustrations.values()){
				output.put(ill.toJSon());
		}
		return output;
	}
	
	public String toJSonString(){
		String illstring = "";
		Collection<Illustration> ills = illustrations.values();
		for(Illustration ill: ills){
			illstring += ill.getFileName();
			illstring += ";";
		}
		if(illstring.length()>0){
			illstring.substring(0, illstring.length()-1);
		}
		return illstring;
	}

	public Illustration get(String illustration_id) {
		if(contains(illustration_id))
			return this.illustrations.get(illustration_id);
		else {
			throw new RuntimeException("Illustration id should exist");
		}
	}

	public boolean contains(String illustration_id) {
		return this.illustrations.containsKey(illustration_id);
	}

	public int size() {
		return illustrations.size();
	}
}
